'''
Input/Output输入与输出
'''

'''文件读写'''

'''读文件'''

'''
绝对路径：absolute path
D:\workspace\python\day06\leave.txt
相对路径：relative path
python\day06\leave.txt
'''

try:
    # 打开文件的方式： r(读), w(覆盖写), a(追加写),b(二进制)
    # 默认编码集继承自当前操作系统
    file = open('python\day06\leave.txt','r',encoding='utf-8') 
    # 读取文件的全部内容
    # content = file.read()
    # print(content)

    # 读取文件中指定数量的字符
    # content = file.read(10)
    # print(content)

    # 读取一行内容
    # line = file.readline()
    # print(line)
    # line = file.readline()
    # print(line)

    # 读取多行
    lines = file.readlines()
    print(lines)

except Exception as e:
    print(e)
finally:
    # 关闭文件
    file.close()


# 以下写法完全等同于上面的代码
with open('python\day06\leave.txt','r',encoding='utf-8') as file:
    content = file.read()
    print(content)

'''写文件'''

# w：如果文件存在，则覆盖原文件，如果文件不存在，则创建一个新文件
with open(r'python\day06\test.txt','w',encoding='utf-8') as file:
    file.write('枯藤老树昏鸦\n') 
    file.write('小桥流水人家\n') 


# 复制图片 day06 -> day05

with open('python\day06\water.jpg','rb') as file:
    content = file.read()
with open('python\day05\water.jpg','wb') as file:
    file.write(content)